package com.factory.iamp.pojo;

import com.alibaba.fastjson.JSONObject;
import com.factory.iamp.utils.helper.SetMethodHelper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class TableDetail {
    private int tid;
    private int ftid;
    private int lid;
    private String pre_cast_id;
    private String sell_id;
    private String name;
    private String creator;
    private UserName creator_info;
    private String handler;
    private UserName handler_info;
    private int type;
    private int header_id;
    private String header_value;
    private List<Integer> body_list;
    private List<TableContent> body_data;
    private int unfinished_content;
    private String body_value;
    private int tail_id;
    private String tail_value;
    private String problem;
    private String file_path;
    private String return_reason;
    private String return_process;
    private Timestamp created_at;
    private Timestamp updated_at;
    private List<Integer> subTable;

    public void setCreator_info(User user) {
        this.creator_info = new UserName(user);
    }

    public void setHandler_info(User user) {
        this.handler_info = new UserName(user);
    }

    public void setHeader_value(Object value) {
        this.header_value = SetMethodHelper.SetString(value);
    }

    public void setBody_value(Object value) {
        this.body_value = SetMethodHelper.SetString(value);
    }

    public void setTail_value(Object value) {
        this.tail_value = SetMethodHelper.SetString(value);
    }

    public void setBody_list(String value) {
        if(value.length() < 1) this.body_list = new ArrayList<>();
        else this.body_list = JSONObject.parseArray(value, Integer.class);
    }

    public void renewBodyData() {
        JSONObject Body_Value = JSONObject.parseObject(this.body_value);
        for(TableContent body_datum : this.body_data) {
            JSONObject content_value = Body_Value.getJSONObject(String.valueOf(body_datum.getOid()));
            JSONObject fill_value = JSONObject.parseObject(body_datum.getContent());
            for (String k : fill_value.keySet()) {
                content_value.put(k, fill_value.getString(k));
            }
            body_datum.setContent(content_value.toString());
        }
        this.problem = "";
        this.body_value = "";
    }
}
